﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ALDIOrganizer.Properties;
using Generator;

namespace ALDIOrganizer
{
    public partial class GeneratorReport : Form
    {
        Report report;
        
        private GeneratorReport()
        {
            InitializeComponent();
        }

        public GeneratorReport(Report pReport)
            : this()
        {
            #region centering the form (to middle-bottom of the GetParent, main form)
            var _parentform = Program.frmMain;
            this.Location = new Point(_parentform.Left + ((_parentform.Width - this.Width) / 2),
                _parentform.Bottom - this.Height);
            this.report = pReport;
            #endregion

            #region "Rota generation #" - set the proper caption depending On the outcome
            var _howWasIt = String.Empty;
            switch (pReport.Outcome)
            {
                case RotaGenerationOutcome.Successful:
                    {
                        _howWasIt = Resources.FORM_GENERATOR_REPORT_Report_WasSuccessful;
                        break;
                    }
                case RotaGenerationOutcome.PartlySuccessful:
                    {
                        _howWasIt = Resources.FORM_GENERATOR_REPORT_Report_PartlyFailed;
                        break;
                    }
                case RotaGenerationOutcome.Failed:
                    {
                        _howWasIt = Resources.FORM_GENERATOR_REPORT_Report_Failed;
                        break;
                    }                
            }
            gbGeneral.Text = gbGeneral.Text.Replace("#",
                _howWasIt);
            #endregion

            lbTime.Text = report.TimeTaken.TotalSeconds.ToString("#.0")
                + "s";
                        
            PopulateFlaws();
            PopulateKeyFlow();
            PopulateOvertimersListview();
        }

        void PopulateFlaws()
        {
            var flaws = String.Empty;

            #region outcome FAILED
            if (report.Outcome == RotaGenerationOutcome.Failed)
            {
                flaws = Resources.FORM_GENERATOR_REPORT_NoSolutionFound;
                var _days = new List<string>();

                for (var day = 0; day < 6; day++)
                {
                    if (report.FailedDays[day])
                    {
                        _days.Add(Time.NameOfDay((Weekday)day));
                    }
                }
                flaws += String.Join(", ", _days.ToArray());
                flaws +=
                    Resources.FORM_GENERATOR_REPORT_NoSolutionFoundAdvice;
                lbFailures.ForeColor = Settings.Default.LABELColour_Error;
            }
            #endregion

            #region outcome PARTLY SUCCESSFUL
            if (report.Outcome == RotaGenerationOutcome.PartlySuccessful)
            {
                if (report.AllFlaws.StoreRunnersHourLimit > 0)
                {
                    flaws =
                        Resources.FORM_GENERATOR_REPORT_StoreRunnersFlaw;
                    flaws =
                        flaws.Replace("#",
                        report.AllFlaws.StoreRunnersHourLimit.ToString());
                    var _people =
                        (from _x in report.StoreRunnerLimitBreakers
                         select SQLiteAccess.GetEmployeeFirstName(_x.Key))
                         .ToArray();
                    flaws = flaws.Replace(
                        @"$",
                        String.Join(", ", _people));
                }
                if (report.AllFlaws.AssistantsHourLimit > 0)
                {
                    if (flaws != String.Empty)
                        flaws += Environment.NewLine;
                    flaws +=
                        Resources.FORM_GENERATOR_REPORT_StoreAssistantsFlaw;
                    flaws = flaws.Replace(
                        "#",
                        report.AllFlaws.AssistantsHourLimit.ToString());
                    var _people = (from breaker in report.StoreAssistantLimitBreakers
                                   select SQLiteAccess.GetEmployeeFirstName(breaker.Key)).ToArray();
                    flaws = flaws.Replace(
                        @"$",
                        String.Join(", ", _people));
                }
            }
            #endregion

            #region outcome SUCCESSFUL
            if (report.Outcome == RotaGenerationOutcome.Successful)
            {
                lbFailures.ForeColor = SystemColors.ControlText;
                flaws = Resources.FORM_GENERATOR_REPORT_NoFlawsFound;
            }
            #endregion

            lbFailures.Text = flaws;
        }

        void PopulateKeyFlow()
        {
            if (report.KeyCycle.On)
            {
                lbNoKeyFlow.Visible = false;
                pbKeyFlow.Visible = true;
                dgridKeyFlow.Visible = true;
                lbKeyFlow.Visible = true;

                lbKeyFlow.Text = report.KeyCycle.Value.ToString()
                    + " / "
                    + report.KeyCycle.Maximum.ToString();
                pbKeyFlow.Maximum = report.KeyCycle.Maximum;
                pbKeyFlow.Value = report.KeyCycle.Value;

                dgridKeyFlow.AutoGenerateColumns = false;

                var _columnDay = new DataGridViewTextBoxColumn();
                _columnDay.DataPropertyName = "Key";
                _columnDay.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                _columnDay.HeaderText = "Day";

                var _columnConnection = new DataGridViewTextBoxColumn();
                _columnConnection.DataPropertyName = "Value";
                _columnConnection.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                _columnConnection.FillWeight = 100;
                _columnConnection.HeaderText = "Connection";

                dgridKeyFlow.Columns.Add(_columnDay);
                dgridKeyFlow.Columns.Add(_columnConnection);
                dgridKeyFlow.DataSource = report.AsDataSource();
                dgridKeyFlow.AutoResizeRows();
            }
            else
            {
                lbNoKeyFlow.Visible = (report.Outcome != RotaGenerationOutcome.Failed);
                pbKeyFlow.Visible = false;
                dgridKeyFlow.Visible = false; 
                dgridKeyFlow.Enabled = false; 
                lbKeyFlow.Visible = false; 
            }
        }

        void PopulateOvertimersListview()
        {
            if (report.GetSortedOvertimes == null |
                report.Outcome == RotaGenerationOutcome.Failed)
            {
                lbNoOvertime.Visible = (report.Outcome != RotaGenerationOutcome.Failed);
                lvTopOvertimers.Visible = false;
                lvTopOvertimers.Enabled = false;
                return;
            }

            lbNoOvertime.Visible = false;
            lvTopOvertimers.Visible = true;
            lvTopOvertimers.Enabled = true;
            
            foreach (var value in report.GetSortedOvertimes)
            {
                var item = new ListViewItem(SQLiteAccess.GetEmployeeFirstName(value.Key));
                var text = Resources.FORM_GENERATOR_REPORT_Overtime.Replace(
                    "#",
                    (value.Value.Key - value.Value.Value).ToString());
                item.SubItems.Add(text);
                lvTopOvertimers.Items.Add(item);
            }

            var width = (lvTopOvertimers.Width -
                (SystemInformation.Border3DSize.Width * 2)) /
                2;
            foreach (int index in new int[] { 0, 1 }) // ;)
            {
                lvTopOvertimers.Columns[index].Width = width;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
